package clothing.database;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import java.sql.Connection;
public class OrderManager {	


		dbase db;

		public OrderManager() {
			db = new dbase();
		}

		public void insert(int supplier_id, String order_date, String expectedD, String status) {
			try {
				Class.forName("com.mysql.jdbc.Driver").newInstance();
				Connection conn = DriverManager.getConnection(
						"jdbc:mysql://199.17.162.76/Clothing", "root", "");

				System.out.println("Connected");
				//Statement st = conn.createStatement();
				
//				
//				String query = "insert into employees values('','" + firstname
//						+ "','" + mi + "','" + lastname + "','" + phone + "','"
//						+ address + "','" + type + "','" + username + "','"
//						+ password + "')";
//				System.out.println(query);
				
				PreparedStatement p = conn.prepareStatement("insert into orders values(?,'',?,?,?)");
				p.setInt(1, supplier_id);
				p.setString(2, order_date);
				p.setString(3, expectedD);
				p.setString(4, status);
				
				p.executeUpdate();
				
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		
		public ArrayList<Order> getList() {
			ArrayList<Order> list = new ArrayList();

			this.db.open();
			try {
				Statement s = this.db.getConn().createStatement();
				String query = "SELECT * from orders";

				ResultSet rs = s.executeQuery(query);

				while (rs.next()) {

					int supplier_id = rs.getInt("Supplier ID");
					int order_id = rs.getInt("Order ID");
					String order_date = rs.getString("Order Date");
					String expectedD = rs.getString("Expected Delivery Day");
					String status = rs.getString("Status");

					Order o = new Order(supplier_id, order_id, order_date, expectedD, status);						

					list.add(o);
				}

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return list;
		}
		
	
		
		public void remove(String uid)
		{
			this.db.open();
			Statement s;
			try {
				s = this.db.conn.createStatement();

				String query ="Delete from orders where uid = '"+uid+"'";
				
				s.execute(query);
				
				System.out.println(uid + "Deleted Successfully");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		public void changeOrderStatus(int id)
		{
			this.db.open();
			Statement s;
			try {
				s = this.db.conn.createStatement();

				String query ="Update orders Set Status =" +
						"'Arrived' where `Order ID` = '"+id+"'" ;
				
				s.execute(query);
				
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	

}
